package br.usp.ime.origami.foldcut.io;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.List;

import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.svggen.SVGGraphics2DIOException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;

import br.usp.ime.origami.foldcut.gui.util.Figure;
import br.usp.ime.origami.foldcut.gui.util.OrigamiShape;

public class SVGSnapshot
{

	public SVGSnapshot()
	{

	}

	public void takeSnapshot(String fileName, List<Figure> background, List<Figure> foreground)
			throws UnsupportedEncodingException, FileNotFoundException, SVGGraphics2DIOException
	{
		DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();

		Document document = domImpl.createDocument(null, "svg", null);

		// Create an instance of the SVG Generator
		SVGGraphics2D g = new SVGGraphics2D(document);

		for (Figure figure : background)
		{
			for (OrigamiShape shape : figure.getShapes())
			{
				g.setColor(OrigamiShape.DrawScheme.getColor(shape.getScheme()));
				g.setStroke(OrigamiShape.DrawScheme.getStroke(shape.getScheme(), -.5f));
				g.draw(shape.getShape());
			}
		}

		for (Figure figure : foreground)
		{
			for (OrigamiShape shape : figure.getShapes())
			{
				g.setColor(OrigamiShape.DrawScheme.getColor(shape.getScheme()));
				g.setStroke(OrigamiShape.DrawScheme.getStroke(shape.getScheme(), 0));
				g.draw(shape.getShape());
			}
		}

		Writer out = new OutputStreamWriter(new FileOutputStream(fileName + ".svg"), "UTF-8");
		g.stream(out, true);

	}
}
